package com.lvtu.monitor.module.web;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lvmama.com.lvtu.common.httpsqs4j.Httpsqs4j;
import com.lvmama.com.lvtu.common.httpsqs4j.HttpsqsClient;
import com.lvmama.com.lvtu.common.httpsqs4j.HttpsqsStatus;
import com.lvmama.com.lvtu.common.httpsqs4j.msg.HttpsqsMessageProtector;
import com.lvtu.monitor.module.web.module.ResultHandler;
import com.lvtu.monitor.utils.Constants;
//import org.nutz.mvc.annotation.At;
//import org.nutz.mvc.annotation.Modules;
//import org.nutz.mvc.annotation.Ok;

/** 
 * @Title: HttpsqsStatus.java 
 * @Package com.lvtu.monitor.module.web 
 * @Description: TODO 
 * @author dengcheng 
 * @date 2015-5-12 上午11:09:02 
 * @version V1.0.0 
 */
@Controller  // 在类上面定义，表明该类为控制器，返回字符串与redirect:xxx
@RequestMapping("/httpsqsStatus")
public class HttpsqsStatusAction {
	
	/**
	 * HttpsqsMessageProtector里面配置的topic
	 */
	private final String [] topics = new String[]{"HttpSqs.mobile", "HttpSqs.dataCollect"};
	
	/**
	 * 没有在HttpsqsMessageProtector里面配置的queue
	 */
	private final String [] extraQueues = new String[]{"VST_SUPP_CNBOOKING_INC_QUEUE"};

		
	@RequestMapping(value="/getStatus",method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ResultHandler getStatus () {
		URI uri;
		try {
			uri = new URI(Constants.getInstance().getProperty("httpsqs_uri"));
			Httpsqs4j.setConnectionInfo(uri.getHost(), uri.getPort(),uri.getPath(), "UTF-8");

			HttpsqsClient client = Httpsqs4j.createNewClient();
			List<HttpsqsStatus> result = new ArrayList<HttpsqsStatus>();
			
			for (String topic : topics) {
				String [] queues = HttpsqsMessageProtector.getInstance().getQueues(topic);
				if (queues != null && queues.length > 0) {
					for (String queue : queues) {
						HttpsqsStatus status = client.getStatus(queue);
						if(status != null){
							result.add(status);
						}
					}
				}
			}
			
			for (String queue : extraQueues) {
				HttpsqsStatus status = client.getStatus(queue);
				if(status != null){
					result.add(status);
				}
			}
			
			/*
			String[] queue = {"ngxlog","test1","test2","test3","test4","test5","test6","test7"};
			for (String string : queue) {
				HttpsqsStatus status = client.getStatus(string);
				if(status!=null){
					result.add(status);
				}
			}
			*/
			
			ResultHandler list = new ResultHandler();
			list.setRows(result);
			//String json = JSON.toJSONString(list) ;
			return list;
			//System.out.println(uri.getHost());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	

	@RequestMapping(value="/toHttpSqsManager")
	public String toHttpSqsManager(){
		return "httpsqs";
	}
	
	@RequestMapping(value="/menu")
	public String menu(){
		return "menu";
	}
		
}
